import { getMiniChatOption } from '@/api/login'

export function setMiniChat(dataInfo) {
  function setMiniChatHtml(window, a, b) {
    window[a] = window[a] || function() {
      (window[a].a = window[a].a || []).push(arguments)
    }
    window[b] = window[b] || function() {
      (window[b].a = window[b].a || []).push(arguments)
    }
    var s = document.createElement('script')
    s.src = 'http://jsapi.weiliaokefu.com/Public/dist/js/jsapi.js?v=' + Math.round(Math.random() * 1000)
    s.async = true
    s.charset = 'UTF-8'
    document.getElementsByTagName('head')[0].appendChild(s)
  }
  /* eslint-disable */
  return getMiniChatOption().then(res => {
    const data = res.data
    if (data.status_code === 200) {
      if (data.data.is_microchat == 1) {
        setMiniChatHtml(window, '_FENBOT', '_FENBOT_API')
        _FENBOT({ cid: res.data.data.cid, type: '0' })
        _FENBOT_API('apiInfo', res.data.data.cid,{
          goodsinfo:dataInfo.goodsinfo,
          orderinfo:dataInfo.orderinfo,
          userinfo:dataInfo.userinfo
        })
      }
    }
  })
}
